package com.zhanqiao.Dao;

import com.alibaba.fastjson.JSON;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;


import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;

import java.util.Map;


public class EchersDao {
    private DataSource dataSource;
    private QueryRunner queryRunner;

    public String getEchars() throws SQLException {
        dataSource = new ComboPooledDataSource();
        queryRunner = new QueryRunner(dataSource);
        Object[] objects = queryRunner.query("select count(*) from user where city='西安'", new ArrayHandler());
        Object[] objects1 = queryRunner.query("select count(*) from user where city='上海'", new ArrayHandler());
        Object[] objects2 = queryRunner.query("select count(*) from user where city='北京'", new ArrayHandler());
        Object[] objects3 = queryRunner.query("select count(*) from user where city='深圳'", new ArrayHandler());
        Object[] objects4 = queryRunner.query("select count(*) from user ", new ArrayHandler());
        Object[] objects5 = queryRunner.query("select count(*) from user where sex='男'", new ArrayHandler());
        Object[] objects6 = queryRunner.query("select count(*) from user where sex='女'", new ArrayHandler());
        Map map = new HashMap();
        map.put("xian", objects);
        map.put("shanghai", objects1);
        map.put("beijing", objects2);
        map.put("shengzheng", objects3);
        map.put("others", objects4);
        map.put("man", objects5);
        map.put("woman", objects6);
        String jsonString = JSON.toJSONString(map);
        return jsonString;

    }
}
